package net.adetech.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by sunfeihu on 2016/10/14.
 */
public class UtilsDate {

    public static Date addHours(Date date, int amount) {
        return org.apache.commons.lang3.time.DateUtils.addHours(date, amount);
    }

    public static Date addDay(Date date, int amount) {
        return org.apache.commons.lang3.time.DateUtils.addDays(date, amount);
    }

    public static Date stringToDate(String strDate) throws ParseException {
        return org.apache.commons.lang3.time.DateUtils.parseDate(strDate, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date stringToShortDate(String strDate) throws ParseException {
        return org.apache.commons.lang3.time.DateUtils.parseDate(strDate, "yyyy-MM-dd");
    }

    public static String getToday(String pattern) {
        return DateFormatUtils.format(Calendar.getInstance(), pattern);
    }

    public static String getToday() {
        return getToday("yyyy-MM-dd HH:mm:ss");
    }


    public static String fmtDate(String pattern, java.sql.Date sqlDate) {
        if (null == sqlDate) {
            return null;
        }
        Date now = new Date(sqlDate.getTime());
        return DateFormatUtils.format(now, pattern);
    }

    public static String fmtDate(String pattern, Date utilDate) {
        if (null == utilDate) {
            return null;
        }
        return DateFormatUtils.format(utilDate, pattern);
    }

    public static String fmtDate(String pattern, Object objDate) {
        if (null == objDate) {
            return null;
        }
        if (objDate instanceof java.sql.Date) {
            return fmtDate(pattern, (java.sql.Date) objDate);
        } else if (objDate instanceof Date) {
            return fmtDate(pattern, (Date) objDate);
        } else {
            return null;
        }
    }

    /**
     * 获取时间差
     * @param start
     * @param end
     * @return
     */
    public static long getXDay(Date start,Date end){
        long startTime = start.getTime();
        long endTime = end.getTime();
        return (endTime-startTime) / (24 * 60 * 60 * 1000);
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    public static String getTimeDistance(Date date) {
        Date now = Calendar.getInstance().getTime();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if (day > 0) {
            return day + "天";
        }
        if (hour > 0) {
            return hour + "小时";
        }
        if (min > 0) {
            return min + "分钟";
        }
        if (sec > 0) {
            return sec + "秒";
        }
        return "";
    }

}
